<?php
class skySession extends Model {
	
	private $pre = '';
	private $lifetime = '360000';
	static $dbconfig = array();
	
	
	public function __construct(){
		require_once ROOT.'./config/db.configs.inc.php';
		self::$dbconfig = &$db;	
		$this->connect(self::$dbconfig['host'],self::$dbconfig['username'],self::$dbconfig['password'],self::$dbconfig['dbname'],self::$dbconfig['pconnect'],self::$dbconfig['char_set']);
		if(file_exists($path = ROOT.'config/gloabel.configs.inc.php')) require_once $path;
		$session_config = $_config['session_config'];
//		$this->pre = $session_config['session_pre'];
//		$this->lifetime = $session_config['session_limtime'];
		session_module_name();
		session_set_save_handler(array(&$this,'sess_open'), array(&$this,'sess_close'), array(&$this,'sess_read'), array(&$this,'sess_write'), array(&$this,'sess_destroy'), array(&$this,'sess_gc'));
		if (session_id() == "") session_start();
	}
	
	
	function sess_open(){
		return true;
	}
	
	function sess_close(){
		$this->sess_gc();
		return true;
	}
	
	function sess_read($key){
		$sql = "select `value` from `sky_sess_tab` where `sess_key`= '$key' and `expiry` > ".time();
		$res = $this->fetchOne($sql);
		return $value = $res['value'];
	}
	
	function sess_write($key,$val){
		$expiry = time() + $this->lifetime;
		if(isset($val) && $val) {
		$sql = "insert into `sky_sess_tab` values('$key','$expiry','$val')";
		$counts = $this->exec($sql);
			if(!$counts) {
				$sqlUp = "update `sky_sess_tab` set `expiry` = $expiry ,`value` = '$val' where `sess_key` = '$key' and `expiry` > ".time();
				$counts = $this->exec($sqlUp);	
				return $counts;
			}
		}
		return $counts;
	}
	
	function sess_destroy($key){
		$sql = "delete from `sky_sess_tab` where `sess_key` = '$key'";
		$result = $this->exec($sql);
		if($result) return $result;
	}
	
	function sess_gc(){
		$sql = 'delete from `sky_sess_tab` where `expiry` <'.time();
		$qid = $this->exec($sql);
		if(!$qid) return false; 
		else return $qid;
	}
}		
?>